#include "zglRenderTarget.h"
#include "zglDrawContext.h"

namespace sgui
{
namespace engine
{
	
void zglRenderTarget::draw(const math::sRectf& rect) const
{
	if (m_texture)
    {
		ssprite2d_Draw(m_texture, rect.X, rect.Y, rect.W, rect.H, 0, 255);
    }
}

void zglRenderTarget::drawClip(const math::sRectf& rect, const math::sRectf& clipRect) const
{
	if (m_texture)
    {
		zglTRect cutRect = m_engine.toRect(clipRect);
		csprite2d_Draw(m_texture, rect.X, rect.Y, rect.W, rect.H, 0, cutRect);
    }
}

void zglRenderTarget::beginRender(sDrawStack& stack) const
{
	rtarget_Set(m_target);
	stack.push(new zglDrawContext(m_engine, math::sRectf(math::sPointf(), getSurfaceSize()), false));
}

void zglRenderTarget::endRender(sDrawStack& stack) const
{
	stack.pop(true);
	rtarget_Set(NULL);
}

zglRenderTarget::zglRenderTarget(zglEngine& engine, const math::sPointf& size) :
	m_engine(engine),
	sRenderTarget(size)
{
	m_texture = tex_CreateZero((u32)size.X, (u32)size.Y, 0, 
		TEX_CLAMP | TEX_FILTER_LINEAR | TEX_CONVERT_TO_POT | TEX_CALCULATE_ALPHA | TEX_REPEAT);
	m_target = rtarget_Add(m_texture, RT_DEFAULT);
}
zglRenderTarget::~zglRenderTarget()
{
	rtarget_Del(&m_target);
}

};
};